<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/img/logo.jpg" alt="">
      </div>
      <el-form class="el-form" :model="loginform" :rules="rules" ref="loginformref">
        <el-form-item prop="username">
          <el-input 
          prefix-icon="el-icon-user-solid"
          v-model="loginform.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
          prefix-icon="el-icon-s-goods"
          v-model="loginform.password"
          type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
export default {
  data() {
    return {
      loginform: {
        username: '',
        password: '',
      },

      rules: {
          username: [
            { required: true, message: '请输入管理员账号', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入管理员密码', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
      }
    }
  },

  methods: {
    reset() {
      this.$refs.loginformref.resetFields();
    },

     login() {
      this.$refs.loginformref.validate( async valid => {
        if(!valid) return;
        let {data} = await axios({
          method: 'post',
          url: '/api/admin',
          data: {
            username: this.loginform.username,
            password: this.loginform.password
          }
        });
        if(data.code){
          this.$message.error(data.message);
        }else{
          this.$message({
            message: '登录成功',
            type: 'success'
          });
          console.log(data);
          this.$router.push("/admin/home");
        }
      });
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background:#fff url(../assets/img/background.jpg) center top no-repeat;
  height: 100%;
}

.login_box {
  width: 35rem;
  height: 20rem;
  background-color: #fff;
  border-radius: 0.3rem;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 1rem #ddd;

  .avatar_box {
    height: 10rem;
    width: 10rem;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 1rem;
    box-shadow: 0 0 1rem #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    img {
      height: 100%;
      width: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }

  
}

.el-form{
  width: 100%;
  position: absolute;
  left: -30%;
  bottom: 0;
  padding: 0 2rem;
  box-sizing: border-box;
}  

.btns{
    display: flex;
    justify-content: flex-end;
  }

</style>
